﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;
using DragDropLibrary;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace ShowMultiple.Plugin
{
    public partial class ShowMultipleImagesView : UserControl
    {
        public WrapPanel _wrap;

        public static readonly DependencyProperty BitmapsAndDescriptionsViewModelsProperty =
            DependencyProperty.Register("BitmapsAndDescriptionsViewModels", typeof(BitmapsAndDescriptionsViewModel), typeof(ShowMultipleImagesView), new PropertyMetadata(null, OnBitmapsAndDescriptionsViewModel1PropertyChanged));

        public BitmapsAndDescriptionsViewModel BitmapsAndDescriptionsViewModels
        {
            get { return (BitmapsAndDescriptionsViewModel)GetValue(BitmapsAndDescriptionsViewModelsProperty); }
            set { SetValue(BitmapsAndDescriptionsViewModelsProperty, value); }
        }

        private static void OnBitmapsAndDescriptionsViewModel1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowMultipleImagesView source = d as ShowMultipleImagesView;
            source.UpdateBitmapsAndDescriptionsViewModel1(e.NewValue as BitmapsAndDescriptionsViewModel, e.OldValue as BitmapsAndDescriptionsViewModel);
        }

        private void UpdateBitmapsAndDescriptionsViewModel1(BitmapsAndDescriptionsViewModel viewModel, BitmapsAndDescriptionsViewModel old)
        {
            if (viewModel != old)
            {
                BitmapsAndDescriptionsViewModels = viewModel;

                if (old != null)
                {
                    old.Clear();
                }

                this.DataContext = BitmapsAndDescriptionsViewModels;
            }
        }

        public ShowMultipleImagesView()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ShowMultipleImagesView_Loaded);
        }

        void ShowMultipleImagesView_Loaded(object sender, RoutedEventArgs e)
        {            
        }

        private void imgMain1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //_wrap.ItemHeight = e.NewSize.Height;
            //_wrap.ItemWidth = e.NewSize.Width;
            //Console.WriteLine(e.NewSize.ToString());
        }

        private void wrap_Loaded(object sender, RoutedEventArgs e)
        {
            _wrap = sender as WrapPanel;
        }

        public void ChangeSize(double wid, double hei)
        {
            _wrap.ItemWidth = wid;
            _wrap.ItemHeight = hei;
            _wrap.InvalidateVisual();
        }

        private void imgMain1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ImageControl ctrl = sender as ImageControl;
            BitmapAndDescriptionViewModel vm = ctrl.Tag as BitmapAndDescriptionViewModel;
            Commands.WantViewFullCommand.Execute(vm.Item.FileName, Window1.MainWindow);
        }

        private void showAvailable_WantAddFilter(object sender, ObjectEventArgs<string> e)
        {
            // HEREHERE
        }

        public void DropCompleted(DropEventArgs<object> e)
        {
            if (e.DroppedOnto == lstItems)
            {
                // HEREHERE: Console.WriteLine("COOL");
            }
        }
    }
}
